
package cribbage.client;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import com.threerings.media.image.BufferedMirage;
import com.threerings.media.image.Mirage;
import com.threerings.parlor.card.client.CardPanel;
import com.threerings.parlor.card.data.Card;
import com.threerings.toybox.util.ToyBoxContext;

public class DeckPanel extends CardPanel {
   private int bigWidth, bigHeight;

   private int smallWidth, smallHeight;

   private BufferedImage bigCards;

   private BufferedImage smallCards;

   private int xLoc, yLoc;

   public DeckPanel (ToyBoxContext ctx) {
      super(ctx.getFrameManager());

      _handSpacing = -2;

      bigCards = ctx.loadImage("media/cards.png");
      smallCards = ctx.loadImage("media/cards_micro.png");
      bigWidth = bigCards.getWidth() / 14;
      bigHeight = bigCards.getHeight() / 4;
      smallWidth = smallCards.getWidth() / 14;
      smallHeight = smallCards.getHeight() / 4;

      _cardWidth = bigWidth;
   }

   @Override
   protected void paintBehind (Graphics2D gfx, Rectangle dirtyRect) {
      gfx.setColor(new Color(0x400080));
      gfx.fill(dirtyRect);
//      super.paintBehind(gfx, dirtyRect);
   }

   @Override
   public void doLayout () {
      xLoc = getWidth() / 2;
      yLoc = getHeight() / 2 - (int)(bigHeight*1.5);

      setHandLocation(xLoc, yLoc);
      adjustHand(1, true);
   }

   public void setHand (int size) {
      setHand(size, 500);
   }

   /**
    * 
    * 
    * @return Returns the cardHeight.
    */
   public int getBigHeight () {
      return bigHeight;
   }

   /**
    * 
    * 
    * @return Returns the _cardWidth.
    */
   public int getBigWidth () {
      return bigWidth;
   }

   @Override
   public Mirage getCardBackImage () {
      return new BufferedMirage(bigCards.getSubimage(13 * bigWidth, 0, bigWidth, bigHeight));
   }

   @Override
   public Mirage getCardImage (Card card) {
      return new BufferedMirage(bigCards.getSubimage((card.getNumber() - 2) * bigWidth, card
            .getSuit()
            * bigHeight, bigWidth, bigHeight));
   }

   @Override
   public Mirage getMicroCardBackImage () {
      return new BufferedMirage(smallCards.getSubimage(0, 13 * smallWidth, smallWidth, smallHeight));
   }

   @Override
   public Mirage getMicroCardImage (Card card) {
      return new BufferedMirage(smallCards.getSubimage((card.getNumber() - 2) * smallWidth, card
            .getSuit()
            * smallHeight, smallWidth, smallHeight));
   }

   public void setUpcard (Card upCard) {
      flyToBoard(upCard, new Point(xLoc, yLoc), new Point(xLoc-bigWidth/2, (int)(yLoc + bigHeight*1.5)), 100L, 0.25F);
   }

}
